<script setup lang="ts">
  import { ref, defineEmits, watch, onMounted } from 'vue';
  import { useBooleanStore } from '@/store';
  import { getCarLocation } from '@/api/les';

  const emit = defineEmits(['updateArray']);
  const booleanStore = useBooleanStore();
  // 表单查询数据
  const code = ref('');
  const occupiedCode = ref('');
  const port = ref('');

  const pageSize = ref(10);
  const pageNumber = ref(1);

  // 表格数据
  async function handleSearch() {
    const newData = await getCarLocation({
      code: code.value,
      type: [5],
      occupiedCode: occupiedCode.value,
      port: port.value,
      current: pageNumber.value,
      size: pageSize.value,
    });
    emit('updateArray', newData);
  }
  // 重置
  function reset() {
    code.value = '';
    occupiedCode.value = '';
    port.value = '';
    handleSearch();
  }
  onMounted(() => {
    handleSearch();
  });
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 106) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  watch(
    () => booleanStore.update,
    () => {
      if (booleanStore.num6 === 108) handleSearch();
    }
  );
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <div>
        <a-input v-model="code" placeholder="库位编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="occupiedCode" placeholder="占用编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="port" placeholder="站点地址" size="large" />
      </div>
    </a-col>
  </a-row>

  <div class="btn-group">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
    <a-button
      type="primary"
      class="btn-reset"
      size="large"
      @click="booleanStore.toggleBool(107)"
      >新增库位</a-button
    >
    <!-- <a-button type="primary" class="btn-reset" size="large">下发计划</a-button> -->
  </div>
</template>

<style scoped>
  .btn-group {
    float: right;
    margin-top: 15px;
  }

  .btn-reset {
    margin-left: 10px;
  }
</style>
